# -*- coding: utf-8 -*

#为Leetcode的题目，生成 md 及 java文件

import os,sys,re

os.chdir("D:\\eclipse-workspace\\LeetCode")#使用绝对位置，在eclipse环境下会跳到另一个目录

print("Current working directory: ")
print(os.getcwd())


########输入难度代号########
print("Please input the difficulty of problem. (e for Easy, m for Medium, h for Hard)")
difficulty,fileName = "",""

while(True):
    difficulty = input()
    
    if difficulty in ("e", "m", "h", 'E', 'M', 'H'):
        break
    else:
        print("Input code wrongly. Please input again.")

    
print("Please input the name of problem. e.g HelloWorld")

dirName = "src\\main\\java\\com\\lun\\"

if difficulty in ('e', 'E'):
    difficulty = 'easy'
elif difficulty in ('m', 'M'):
    difficulty = 'medium'
else:
    difficulty = 'hard'

dirName = dirName + difficulty + "\\"
noteDirName = "note\\%s\\" % difficulty


########输入文件名########
while(True):
    fileName = input()
    words = fileName.strip().split()

    fileName = ''
    for word in words:
        for i in range(len(word)):
            c = word[i]
            if i == 0:
                c = c.upper()
            fileName += c

    print("After deleting white space characters, the file name will be :")
    print(fileName)
    print()

    print("Are you satisfied with the file name ? If not, press n/N to input again, press others to continue.")
    ans = input()

    if ans in ('n', 'N'):
        print("\nPlease input the name of problem. e.g HelloWorld")
        continue

    if re.match(r"^[A-Z][A-Za-z0-9]*", fileName):
        if os.path.exists(noteDirName + fileName + ".md"):
            print(dirName + fileName + ".md's existed. Please check.")
            continue
        break
    else:
        print("Input file name is illegal. Please input again.")
    

########生成Md文件########
mdFile = open(noteDirName + fileName + ".md","w")
mdFileContent = '''# %s

## Topic

- 

## Description

[link]()



## Analysis



## Submissions

```java

```

## Test

```java

```

## Code

[Source Code](../../src/main/java/com/lun/%s/%s.java)

[Test Code](../../src/test/java/com/lun/%s/%sTest.java)
''' % (fileName, difficulty, fileName, difficulty, fileName)

mdFile.write(mdFileContent)
mdFile.close()

########生成Java文件########
javaFile = open(dirName + fileName + ".java","w")
javaFileContent = '''package com.lun.%s;

public class %s {

}
'''%(difficulty, fileName)
javaFile.write(javaFileContent)
javaFile.close()


########生成Java测试文件########
testFile = open((dirName + fileName + "Test.java").replace('main','test'),"w")
testFileContent = '''package com.lun.%s;

import static org.junit.Assert.*;
import org.junit.Test;

public class %sTest {

    @Test
    public void test() {
        %s %sObj = new %s();

    }
}
'''%(difficulty, fileName, fileName, fileName[0].lower(), fileName)
testFile.write(testFileContent)
testFile.close()


########最后########
print("Create %s.md and %s.java and %sTest.java successfully! Press Enter to exit."%(fileName, fileName, fileName))

try:
    input()
    print("Bye!")
except:
    print("Bye!")
